package com.dycn.posdashboard.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.dycn.posdashboard.data.JsonResponseData;
import com.dycn.posdashboard.data.PaginatedInfor;
import com.dycn.posdashboard.data.PosTransactionDataStats;
import com.dycn.posdashboard.data.ServicesResultObject;
import com.dycn.posdashboard.data.StatsData001;
import com.dycn.posdashboard.data.StatsData002;
import com.dycn.posdashboard.data.StatsData003;
import com.dycn.posdashboard.data.StatsTimeUnit;
import com.dycn.posdashboard.dbhelper.DataBaseUser;
import com.dycn.posdashboard.services.PosTradeServices;
import com.dycn.posdashboard.util.JSONObjectHelp;
import com.opensymphony.xwork2.ActionSupport;

public class StatsTransactionDatasAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6434629048833189767L;

	protected final Log log = LogFactory.getLog(getClass());
    
	private PosTradeServices posTradeServices;
    private JsonResponseData<Object> commonResponseDataJson;
	public PosTradeServices getPosTradeServices() {
		return posTradeServices;
	}
	public void setPosTradeServices(PosTradeServices posTradeServices) {
		this.posTradeServices = posTradeServices;
	}
	public JsonResponseData<Object> getCommonResponseDataJson() {
		return commonResponseDataJson;
	}
	public void setCommonResponseDataJson(
			JsonResponseData<Object> commonResponseDataJson) {
		this.commonResponseDataJson = commonResponseDataJson;
	}
	
	
	/**
	 * 以卡为单位的消费次数的统计
	 * @return
	 */
	public String getStats003OfPosOffline(){
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	String branchNo=request.getParameter("branchNo");//信息统计单位
    	String terminalMerchantNo=request.getParameter("terNo");//想要返回的结果页码
    	
    	String sDate=request.getParameter("sDate");//想要返回的结果页码
    	String eDate=request.getParameter("eDate");//想要返回的结果页码
    	String statsDateRange=request.getParameter("timeRange");
    	try{
				
				//StatsTimeUnit statsTimeUnit
//    		DateFormat format1 = new SimpleDateFormat("yyyyMMdd");  
//			Date startDateTime=format1.parse(sDate);
//			Date endDateTime=format1.parse(eDate);
//			Date tmp;
//			if(startDateTime.after(endDateTime)){
//				tmp=startDateTime;
//				endDateTime=startDateTime;
//				startDateTime=tmp;
//			}
//			
	    	int statsDateRangeType=2;//0-》全部 1 ->本月 2->本年
//	    	if(statsDateRange==null||statsDateRange.trim().equals("")){
//	    		statsDateRangeType=0;
//	    	}else {
//	    		statsDateRangeType=Integer.parseInt(statsDateRange);
//	    	}
			Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
			Date endDateTime=null;
			if(statsDateRangeType==1){
				startDateTime=DateUtils.truncate(new Date(),Calendar.MONTH);
				endDateTime=null;
			}else if(statsDateRangeType==2){
				startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
				endDateTime=null;
			}else{
				startDateTime=null;
				endDateTime=null;
			}

			ServicesResultObject<List<StatsData003>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.queryStatsData003(DataBaseUser.MAIN_USER,branchNo, terminalMerchantNo, startDateTime, endDateTime);
			
    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
        		commonResponseDataJson.setData(null);
        	}else{

        		commonResponseDataJson.setMsg("");
        		commonResponseDataJson.setSuccess(true);
        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
        		commonResponseDataJson.setData(servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj());

//        		List<StatsData003> resultList=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
//        		for(StatsData003 someStatsData003:resultList){
//        			someStatsData003.setTimes1(someStatsData003.getTimes1()/100);
//        		}
        		
          		log.debug("getStats003OfPosOffline json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());

        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
    	
		
		return SUCCESS;
	}
	
	
	/**
	 * 以分公司为单位统计消息金额及次数等信息i
	 * @return
	 */
	public String getStats002OfPosOffline(){
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	String branchNo=request.getParameter("branchNo");//信息统计单位
    	String terminalMerchantNo=request.getParameter("terNo");//想要返回的结果页码
    	
    	String sDate=request.getParameter("sDate");//想要返回的结果页码
    	String eDate=request.getParameter("eDate");//想要返回的结果页码
    	String statsDateRange=request.getParameter("timeRange");
    	log.info("timeRange::"+statsDateRange);
    	try{
				
				//StatsTimeUnit statsTimeUnit
//    		DateFormat format1 = new SimpleDateFormat("yyyyMMdd");  
//			Date startDateTime=format1.parse(sDate);
//			Date endDateTime=format1.parse(eDate);
//			Date tmp;
//			if(startDateTime.after(endDateTime)){
//				tmp=startDateTime;
//				endDateTime=startDateTime;
//				startDateTime=tmp;
//			}
	    	int statsDateRangeType=0;//0-》全部 1 ->本月 2->本年
	    	if(statsDateRange==null||statsDateRange.trim().equals("")){
	    		statsDateRangeType=0;
	    	}else {
	    		statsDateRangeType=Integer.parseInt(statsDateRange);
	    	}
			Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
			Date endDateTime=null;
			if(statsDateRangeType==1){
				startDateTime=DateUtils.truncate(new Date(),Calendar.MONTH);
				endDateTime=null;
			}else if(statsDateRangeType==2){
				startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
				endDateTime=null;
			}else{
				startDateTime=null;
				endDateTime=null;
			}
			
			ServicesResultObject<List<StatsData002>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.queryStatsData002(DataBaseUser.MAIN_USER,branchNo, terminalMerchantNo, startDateTime, endDateTime);
			
    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
        		commonResponseDataJson.setData(null);
        	}else{

        		commonResponseDataJson.setMsg("");
        		commonResponseDataJson.setSuccess(true);
        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
        		int length=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj().size();
        		if(length>1){
        			//去掉最后一个，rollup的合计统计值
        			commonResponseDataJson.setData(servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj().subList(0, length-1));
        		}else{
        			commonResponseDataJson.setData(servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj());
        		}
        		

        		
          		log.debug("getStats002OfPosOffline json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());

        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
    	
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 */
	public String getTotalConsumingAndTradesInfoChartOfMonthly(){
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	String branchNo=request.getParameter("branchNo");//信息统计单位
    	String terminalMerchantNo=request.getParameter("terNo");//想要返回的结果页码
    	
//   	String sDate=request.getParameter("sDate");//想要返回的结果页码
//    	String eDate=request.getParameter("eDate");//想要返回的结果页码
//    	String statsDateRange=request.getParameter("timeRange");
    	try{
				
				//StatsTimeUnit statsTimeUnit
//	    	int statsDateRangeType=2;//0-》全部 1 ->本月 2->本年
//	    	if(statsDateRange==null||statsDateRange.trim().equals("")){
//	    		statsDateRangeType=0;
//	    	}else {
//	    		statsDateRangeType=Integer.parseInt(statsDateRange);
//	    	}
	    	StatsTimeUnit stateTimeUnit=null;
			Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
			Date endDateTime=null;
			//group by month
			startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
			endDateTime=null;
			stateTimeUnit=StatsTimeUnit.MONTH;
			
			ServicesResultObject<List<StatsData001>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.queryStatsData(DataBaseUser.MAIN_USER,null, terminalMerchantNo, startDateTime, endDateTime,stateTimeUnit);
			
    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
        		commonResponseDataJson.setData(null);
        	}else{

        		commonResponseDataJson.setMsg("");
        		commonResponseDataJson.setSuccess(true);
        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
        		List<StatsData001> resultList=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
        		Collections.sort(resultList,new Comparator<StatsData001>(){

					@Override
					public int compare(StatsData001 arg0, StatsData001 arg1) {
						// TODO Auto-generated method stub

						return arg0.getDate().compareTo(arg1.getDate());
					}
        			
        		});

        		commonResponseDataJson.setData(servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj());

        		
          		log.debug("getStats001OfPosOffline json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());

        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
		
		return SUCCESS;
	}
	
	public String getAverageTransactionMoneyOfMonthly(){
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	String branchNo=request.getParameter("branchNo");//信息统计单位
    	String terminalMerchantNo=request.getParameter("terNo");//想要返回的结果页码
    	
//   	String sDate=request.getParameter("sDate");//想要返回的结果页码
//    	String eDate=request.getParameter("eDate");//想要返回的结果页码
//    	String statsDateRange=request.getParameter("timeRange");
    	try{
				
				//StatsTimeUnit statsTimeUnit
//	    	int statsDateRangeType=2;//0-》全部 1 ->本月 2->本年
//	    	if(statsDateRange==null||statsDateRange.trim().equals("")){
//	    		statsDateRangeType=0;
//	    	}else {
//	    		statsDateRangeType=Integer.parseInt(statsDateRange);
//	    	}
	    	StatsTimeUnit stateTimeUnit=null;
			Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
			Date endDateTime=null;
			//group by month
			startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
			endDateTime=null;
			stateTimeUnit=StatsTimeUnit.MONTH;
			
			ServicesResultObject<List<StatsData001>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.queryStatsData(DataBaseUser.MAIN_USER,null, terminalMerchantNo, startDateTime, endDateTime,stateTimeUnit);
			
    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
        		commonResponseDataJson.setData(null);
        	}else{

        		commonResponseDataJson.setMsg("");
        		commonResponseDataJson.setSuccess(true);
        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
        		List<StatsData001> resultList=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
        		Collections.sort(resultList,new Comparator<StatsData001>(){

					@Override
					public int compare(StatsData001 arg0, StatsData001 arg1) {
						// TODO Auto-generated method stub

						return arg0.getDate().compareTo(arg1.getDate());
					}
        			
        		});

        		double averageOfAll=0.00;
        		double totalMoney=0.00;
        		double trades=0;
        		for(StatsData001 someStatsData001:resultList){
        			totalMoney=totalMoney+someStatsData001.getTotalTradeMoney();
        			trades=trades+someStatsData001.getTotalTrades();
        		}
        		averageOfAll=totalMoney/trades;
        		for(StatsData001 someStatsData001:resultList){
        			someStatsData001.setAllAverage(averageOfAll);
        		}
        		
        		
        		commonResponseDataJson.setData(servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj());

        		
          		log.debug("getStats001OfPosOffline json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());

        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
		
		return SUCCESS;
	}
	
	public String getStats001OfSpecBranch(){
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	String branchNo=request.getParameter("branchNo");//信息统计单位
    	//String terminalMerchantNo=request.getParameter("terNo");//想要返回的结果页码
    	
    	String sDate=request.getParameter("sDate");//想要返回的结果页码
    	String eDate=request.getParameter("eDate");//想要返回的结果页码
    	String statsDateRange=request.getParameter("timeRange");
    	statsDateRange="2";
    	try{
				
				//StatsTimeUnit statsTimeUnit
	    	int statsDateRangeType=0;//0-》全部 1 ->本月 2->本年
	    	if(statsDateRange==null||statsDateRange.trim().equals("")){
	    		statsDateRangeType=0;
	    	}else {
	    		statsDateRangeType=Integer.parseInt(statsDateRange);
	    	}
	    	StatsTimeUnit stateTimeUnit=null;
			Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
			Date endDateTime=null;

			List<Integer> yearRang=new ArrayList<Integer>();
			Calendar temp=Calendar.getInstance();
			temp.setTime(new Date());
			yearRang.add(temp.get(Calendar.YEAR));
			temp.add(Calendar.YEAR, -1);
			yearRang.add(temp.get(Calendar.YEAR));
			temp.add(Calendar.YEAR, -1);
			yearRang.add(temp.get(Calendar.YEAR));
			
			startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
			startDateTime=DateUtils.addYears(startDateTime, -2);
			endDateTime=null;
			stateTimeUnit=StatsTimeUnit.MONTH;
			
			
			
			       			
			int year=temp.get(Calendar.YEAR);
			int month=temp.get(Calendar.MONTH);
			

			ServicesResultObject<List<StatsData001>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.queryStatsData(DataBaseUser.MAIN_USER,branchNo, null, startDateTime, endDateTime,stateTimeUnit);
			
    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
        		commonResponseDataJson.setData(null);
        	}else{

        		commonResponseDataJson.setMsg("");
        		commonResponseDataJson.setSuccess(true);
        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
        		List<StatsData001> resultList=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
        		Collections.sort(resultList,new Comparator<StatsData001>(){

					@Override
					public int compare(StatsData001 arg0, StatsData001 arg1) {
						// TODO Auto-generated method stub

						return arg0.getDate().compareTo(arg1.getDate());
					}
        			
        		});
        		//someStatsData001
        		HashMap<Integer,double[]> resulHashMap=new HashMap<Integer,double[]>();
        		
        		String key1="year1";
        		String key2="year2";
        		String key3="year3";
        		for(StatsData001 someStatsData001:resultList){
        			Date specDate=someStatsData001.getDate();
        			Calendar temp00=Calendar.getInstance();
        			temp00.setTime(specDate);
        			       			
        			int year00=temp00.get(Calendar.YEAR);
        			int month00=temp00.get(Calendar.MONTH);
        			
        			double[] monthMoneyArray=resulHashMap.get(new Integer(year00));
        			if(monthMoneyArray==null){
        				monthMoneyArray=new double[12];
        				resulHashMap.put(new Integer(year00), monthMoneyArray);
        			}
        			monthMoneyArray[month00]=someStatsData001.getTotalTradeMoney();
        		}
        		
        		
        		List<JSONObject> reslutJSONObjectList=new ArrayList<JSONObject>();
        		for(int i=0;i<12;i++){
        			JSONObject someJSONObject=new JSONObject();
        			someJSONObject.element("month", i+1);
        			someJSONObject.element("monthStr", (i+1)+"月");

            		for(int k=0;k<yearRang.size();k++){
            			
            			double[] monthMoneyArray=resulHashMap.get(yearRang.get(k));
            			if(monthMoneyArray!=null){
                			someJSONObject.element("data"+(k+1), monthMoneyArray[i]);
                			someJSONObject.element("aata"+(k+1)+"str", yearRang.get(k));
            			}else{
                			someJSONObject.element("data"+(k+1), 0.00);
                			someJSONObject.element("data"+(k+1)+"str", yearRang.get(k));
            			}

            		}
            		
        			
            		reslutJSONObjectList.add(someJSONObject);
   
        			
        		}

        		//commonResponseDataJson.setData(servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj());

        		commonResponseDataJson.setData(reslutJSONObjectList);
        		
          		log.debug("getStats001OfSpecBranch json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());

        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
    	
    	return SUCCESS;
	}
	
	/**
	 * 统计交易信息
	 * @return
	 */
	public String getStats001OfPosOffline(){
		
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	String branchNo=request.getParameter("branchNo");//信息统计单位
    	String terminalMerchantNo=request.getParameter("terNo");//想要返回的结果页码
    	
    	String sDate=request.getParameter("sDate");//想要返回的结果页码
    	String eDate=request.getParameter("eDate");//想要返回的结果页码
    	String statsDateRange=request.getParameter("timeRange");
    	try{
				
				//StatsTimeUnit statsTimeUnit
	    	int statsDateRangeType=0;//0-》全部 1 ->本月 2->本年
	    	if(statsDateRange==null||statsDateRange.trim().equals("")){
	    		statsDateRangeType=0;
	    	}else {
	    		statsDateRangeType=Integer.parseInt(statsDateRange);
	    	}
	    	StatsTimeUnit stateTimeUnit=null;
			Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
			Date endDateTime=null;
			if(statsDateRangeType==1){
				//group by day
				startDateTime=DateUtils.truncate(new Date(),Calendar.MONTH);
				endDateTime=null;
				stateTimeUnit=StatsTimeUnit.DAY;
			}else if(statsDateRangeType==2){
				//group by month
				startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
				endDateTime=null;
				stateTimeUnit=StatsTimeUnit.MONTH;
			}else{
				//group by year
				startDateTime=null;
				endDateTime=null;
				stateTimeUnit=StatsTimeUnit.YEAR;
			}

			ServicesResultObject<List<StatsData001>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.queryStatsData(DataBaseUser.MAIN_USER,null, null, startDateTime, endDateTime,stateTimeUnit);
			
    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
        		commonResponseDataJson.setData(null);
        	}else{

        		commonResponseDataJson.setMsg("");
        		commonResponseDataJson.setSuccess(true);
        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
        		List<StatsData001> resultList=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
        		Collections.sort(resultList,new Comparator<StatsData001>(){

					@Override
					public int compare(StatsData001 arg0, StatsData001 arg1) {
						// TODO Auto-generated method stub

						return arg0.getDate().compareTo(arg1.getDate());
					}
        			
        		});
        		

        		commonResponseDataJson.setData(servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj());

        		
          		log.debug("getStats001OfPosOffline json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());

        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
    	
    	return SUCCESS;
	}
    
	
	public String getMerchantPosTradeStatsForSpecBranch(){
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	//String groupby=request.getParameter("groupby");//信息统计单位
    	String startPageStr=request.getParameter("page");//想要返回的结果页码
    	String statsDateRange=request.getParameter("timeRange");
    	String specBranchNo=request.getParameter("branchNo");
 
    	String limitstr=request.getParameter("limit");
    	String totalRecordStr=request.getParameter("totalRecord");
    	
		if(specBranchNo==null||specBranchNo.trim().equals("")){
			commonResponseDataJson.setResultCode(JsonResponseData.LOGIC_ERROR);
		}else{
			try{

		    	int pageSize=((limitstr==null||limitstr.trim().equals(""))?20:Integer.parseInt(limitstr));
		    	int startPage=0;

		    	int statsDateRangeType=0;//0-》全部 1 ->本月 2->本年
				//pageSize=Integer.parseInt(pageSizeStr);
		    	
		    	if(startPageStr==null||startPageStr.trim().equals("")||startPageStr.trim().equals("0")){
		    		
		    	}
				startPage=Integer.parseInt((startPageStr==null||startPageStr.trim().equals(""))?"1":startPageStr);
		    	if(statsDateRange==null||statsDateRange.trim().equals("")){
		    		statsDateRangeType=0;
		    	}else {
		    		statsDateRangeType=Integer.parseInt(statsDateRange);
		    	}
				
				PaginatedInfor<PosTransactionDataStats> nonDataPaginatedInfor=new PaginatedInfor<PosTransactionDataStats>();
				nonDataPaginatedInfor.setPageId(startPage);
				nonDataPaginatedInfor.setPageRecords(pageSize);
				if(totalRecordStr==null||totalRecordStr.trim().equals("")){
					nonDataPaginatedInfor.setTotalRecords(-1);
				}else{
					nonDataPaginatedInfor.setTotalRecords(Integer.parseInt(totalRecordStr));
				}
				
				//StatsTimeUnit statsTimeUnit
				Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
				Date endDateTime=null;
				if(statsDateRangeType==1){
					startDateTime=DateUtils.truncate(new Date(),Calendar.MONTH);
					endDateTime=null;
				}else if(statsDateRangeType==2){
					startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
					endDateTime=null;
				}else{
					startDateTime=null;
					endDateTime=null;
				}
				ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.getMerchantPosTradeStatsByBranch(DataBaseUser.MAIN_USER,specBranchNo,startDateTime, endDateTime, nonDataPaginatedInfor);
				
	    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
	        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
	           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
	        		commonResponseDataJson.setData(null);
	        	}else{
	        		PaginatedInfor<PosTransactionDataStats> posTransactionDataPageInfor=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
	         		  
	        		List<PosTransactionDataStats> currrentPagePosTransactionDataList=posTransactionDataPageInfor.getDataList();
	        		int totalRecords=posTransactionDataPageInfor.getTotalRecords();
	        		int totalPages=1;
	    			if(totalRecords%pageSize!=0){
						totalPages=totalRecords/pageSize+1;
					}else{
						totalPages=totalRecords/pageSize;
					}
	    			
	    			if(startPage>totalPages){
	    				startPage=totalPages;
	    			}
	    			
	        		commonResponseDataJson.setMsg("");
	        		commonResponseDataJson.setSuccess(true);
	        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
	        		commonResponseDataJson.setData(currrentPagePosTransactionDataList);
	        		
	        		commonResponseDataJson.setRecordsPer(pageSize);
	        		commonResponseDataJson.setPageId(startPage);
	        		commonResponseDataJson.setTotalRecords(totalRecords);
	        		commonResponseDataJson.setTotalPage(totalPages);
	        		
	          		log.debug("getPosTradeStatsByCard json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());
	
	        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
		}
		return SUCCESS;
	}
	
	/**
	 * 特定分公司的POS交易信息统计
	 * http://localhost:8080/pos-dashboard/statsSpecBranchTrades.action?page=1&&timeRange=2&&branchNo=906290053110002
	 * @return
	 */
	public String getPosTradeStatsForSpecBranch(){
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	//String groupby=request.getParameter("groupby");//信息统计单位
    	String startPageStr=request.getParameter("page");//想要返回的结果页码
    	String statsDateRange=request.getParameter("timeRange");
    	String specBranchNo=request.getParameter("branchNo");
 
    	String limitstr=request.getParameter("limit");
    	String totalRecordStr=request.getParameter("totalRecord");
    	
		if(specBranchNo==null||specBranchNo.trim().equals("")){
			commonResponseDataJson.setResultCode(JsonResponseData.LOGIC_ERROR);
		}else{
			try{

		    	int pageSize=((limitstr==null||limitstr.trim().equals(""))?20:Integer.parseInt(limitstr));
		    	int startPage=0;

		    	int statsDateRangeType=0;//0-》全部 1 ->本月 2->本年
				//pageSize=Integer.parseInt(pageSizeStr);
		    	
		    	if(startPageStr==null||startPageStr.trim().equals("")||startPageStr.trim().equals("0")){
		    		
		    	}
				startPage=Integer.parseInt((startPageStr==null||startPageStr.trim().equals(""))?"1":startPageStr);
		    	if(statsDateRange==null||statsDateRange.trim().equals("")){
		    		statsDateRangeType=0;
		    	}else {
		    		statsDateRangeType=Integer.parseInt(statsDateRange);
		    	}
				
				PaginatedInfor<PosTransactionDataStats> nonDataPaginatedInfor=new PaginatedInfor<PosTransactionDataStats>();
				nonDataPaginatedInfor.setPageId(startPage);
				nonDataPaginatedInfor.setPageRecords(pageSize);
				if(totalRecordStr==null||totalRecordStr.trim().equals("")){
					nonDataPaginatedInfor.setTotalRecords(-1);
				}else{
					nonDataPaginatedInfor.setTotalRecords(Integer.parseInt(totalRecordStr));
				}
				
				//StatsTimeUnit statsTimeUnit
				Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
				Date endDateTime=null;
				if(statsDateRangeType==1){
					startDateTime=DateUtils.truncate(new Date(),Calendar.MONTH);
					endDateTime=null;
				}else if(statsDateRangeType==2){
					startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
					endDateTime=null;
				}else{
					startDateTime=null;
					endDateTime=null;
				}
				ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.getPosTradeStatsByBranch(DataBaseUser.MAIN_USER,specBranchNo,startDateTime, endDateTime, nonDataPaginatedInfor);
				
	    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
	        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
	           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
	        		commonResponseDataJson.setData(null);
	        	}else{
	        		PaginatedInfor<PosTransactionDataStats> posTransactionDataPageInfor=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
	         		  
	        		List<PosTransactionDataStats> currrentPagePosTransactionDataList=posTransactionDataPageInfor.getDataList();
	        		int totalRecords=posTransactionDataPageInfor.getTotalRecords();
	        		int totalPages=1;
	    			if(totalRecords%pageSize!=0){
						totalPages=totalRecords/pageSize+1;
					}else{
						totalPages=totalRecords/pageSize;
					}
	    			
	    			if(startPage>totalPages){
	    				startPage=totalPages;
	    			}
	    			
	        		commonResponseDataJson.setMsg("");
	        		commonResponseDataJson.setSuccess(true);
	        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
	        		commonResponseDataJson.setData(currrentPagePosTransactionDataList);
	        		
	        		commonResponseDataJson.setRecordsPer(pageSize);
	        		commonResponseDataJson.setPageId(startPage);
	        		commonResponseDataJson.setTotalRecords(totalRecords);
	        		commonResponseDataJson.setTotalPage(totalPages);
	        		
	          		log.debug("getPosTradeStatsByCard json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());
	
	        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
		}
		return SUCCESS;
	}
	
	/**
	 * 特定商户的POS交易信息统计
	 * http://localhost:8080/pos-dashboard/statsSpecTerminalMerchantTrades.action?page=1&&timeRange=2&&terNo=90600004
	 * @return
	 */
	public String getPosTradeStatsForSpecTerminalMerchant(){
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	//String groupby=request.getParameter("groupby");//信息统计单位
    	String startPageStr=request.getParameter("page");//想要返回的结果页码
    	String statsDateRange=request.getParameter("timeRange");
    	String terminalMerchantNo=request.getParameter("terNo");
 
    	String limitstr=request.getParameter("limit");
    	String totalRecordStr=request.getParameter("totalRecord");
    	
		if(terminalMerchantNo==null||terminalMerchantNo.trim().equals("")){
			commonResponseDataJson.setResultCode(JsonResponseData.LOGIC_ERROR);
		}else{
			try{

		    	int pageSize=((limitstr==null||limitstr.trim().equals(""))?20:Integer.parseInt(limitstr));
		    	int startPage=0;

		    	int statsDateRangeType=0;//0-》全部 1 ->本月 2->本年
				//pageSize=Integer.parseInt(pageSizeStr);
		    	
		    	if(startPageStr==null||startPageStr.trim().equals("")||startPageStr.trim().equals("0")){
		    		
		    	}
				startPage=Integer.parseInt((startPageStr==null||startPageStr.trim().equals(""))?"1":startPageStr);
		    	if(statsDateRange==null||statsDateRange.trim().equals("")){
		    		statsDateRangeType=0;
		    	}else {
		    		statsDateRangeType=Integer.parseInt(statsDateRange);
		    	}
				
				PaginatedInfor<PosTransactionDataStats> nonDataPaginatedInfor=new PaginatedInfor<PosTransactionDataStats>();
				nonDataPaginatedInfor.setPageId(startPage);
				nonDataPaginatedInfor.setPageRecords(pageSize);
				if(totalRecordStr==null||totalRecordStr.trim().equals("")){
					nonDataPaginatedInfor.setTotalRecords(-1);
				}else{
					nonDataPaginatedInfor.setTotalRecords(Integer.parseInt(totalRecordStr));
				}
				
				//StatsTimeUnit statsTimeUnit
				Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
				Date endDateTime=null;
				if(statsDateRangeType==1){
					startDateTime=DateUtils.truncate(new Date(),Calendar.MONTH);
					endDateTime=null;
				}else if(statsDateRangeType==2){
					startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
					endDateTime=null;
				}else{
					startDateTime=null;
					endDateTime=null;
				}
				ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.getPosTradeStatsByTerminalMerchantNo(DataBaseUser.MAIN_USER,terminalMerchantNo,startDateTime, endDateTime, nonDataPaginatedInfor);
				
	    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
	        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
	           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
	        		commonResponseDataJson.setData(null);
	        	}else{
	        		PaginatedInfor<PosTransactionDataStats> posTransactionDataPageInfor=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
	         		  
	        		List<PosTransactionDataStats> currrentPagePosTransactionDataList=posTransactionDataPageInfor.getDataList();
	        		int totalRecords=posTransactionDataPageInfor.getTotalRecords();
	        		int totalPages=1;
	    			if(totalRecords%pageSize!=0){
						totalPages=totalRecords/pageSize+1;
					}else{
						totalPages=totalRecords/pageSize;
					}
	    			
	    			if(startPage>totalPages){
	    				startPage=totalPages;
	    			}
	    			
	        		commonResponseDataJson.setMsg("");
	        		commonResponseDataJson.setSuccess(true);
	        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
	        		commonResponseDataJson.setData(currrentPagePosTransactionDataList);
	        		
	        		commonResponseDataJson.setRecordsPer(pageSize);
	        		commonResponseDataJson.setPageId(startPage);
	        		commonResponseDataJson.setTotalRecords(totalRecords);
	        		commonResponseDataJson.setTotalPage(totalPages);
	        		
	          		log.debug("getPosTradeStatsByCard json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());
	
	        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
		}
		return SUCCESS;
	}
	
	/**
	 * 以用户卡号为单位统计交易信息
	 * @return
	 */
	public String getPosTradeStatsForPosOffline(){
		
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
		commonResponseDataJson.setMsg("参数提交错误~~~!!!!!!");
		commonResponseDataJson.setData(null);
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	//String groupby=request.getParameter("groupby");//信息统计单位
    	String startPageStr=request.getParameter("page");//想要返回的结果页码
    	String statsDateRange=request.getParameter("timeRange");
    	
    	String limitstr=request.getParameter("limit");
    	String totalRecordStr=request.getParameter("totalRecord");
    	String branchNo=request.getParameter("branchNo");
    	String terNo=request.getParameter("terNo");

    	
    	try{

		    	int pageSize=((limitstr==null||limitstr.trim().equals(""))?20:Integer.parseInt(limitstr));
		    	int startPage=0;
		    	int statsDateRangeType=0;//0-》全部 1 ->本月 2->本年
				//pageSize=Integer.parseInt(pageSizeStr);
				startPage=Integer.parseInt((startPageStr==null||startPageStr.trim().equals(""))?"1":startPageStr);
		    	if(statsDateRange==null||statsDateRange.trim().equals("")){
		    		statsDateRangeType=0;
		    	}else {
		    		statsDateRangeType=Integer.parseInt(statsDateRange);
		    	}
				
				PaginatedInfor<PosTransactionDataStats> nonDataPaginatedInfor=new PaginatedInfor<PosTransactionDataStats>();
				nonDataPaginatedInfor.setPageId(startPage);
				nonDataPaginatedInfor.setPageRecords(pageSize);
				if(totalRecordStr==null||totalRecordStr.trim().equals("")){
					nonDataPaginatedInfor.setTotalRecords(-1);
				}else{
					nonDataPaginatedInfor.setTotalRecords(Integer.parseInt(totalRecordStr));
				}
				
				//StatsTimeUnit statsTimeUnit
				Date startDateTime=DateUtils.truncate(new Date(),Calendar.HOUR_OF_DAY);
				Date endDateTime=null;
				if(statsDateRangeType==1){
					startDateTime=DateUtils.truncate(new Date(),Calendar.MONTH);
					endDateTime=null;
				}else if(statsDateRangeType==2){
					startDateTime=DateUtils.truncate(new Date(),Calendar.YEAR);
					endDateTime=null;
				}else{
					startDateTime=null;
					endDateTime=null;
				}
				ServicesResultObject<PaginatedInfor<PosTransactionDataStats>> servicesResultObjectOfgetPosTransactionDataListBy=null;
				if(terNo!=null&&!terNo.trim().equals("")){
					servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.getPosTradeStatsByTerminalMerchantNo(DataBaseUser.MAIN_USER, terNo, startDateTime, endDateTime, nonDataPaginatedInfor);
				}else if(branchNo!=null&&!branchNo.trim().equals("")){
					servicesResultObjectOfgetPosTransactionDataListBy=this.posTradeServices.getPosTradeStatsByBranch(DataBaseUser.MAIN_USER, branchNo, startDateTime, endDateTime, nonDataPaginatedInfor);
					
				}else{
					servicesResultObjectOfgetPosTransactionDataListBy=
							this.posTradeServices.getPosTradeStatsByCardNo(DataBaseUser.MAIN_USER,startDateTime, endDateTime,null, nonDataPaginatedInfor);
				}
			
//		    	String branchNo=request.getParameter("branchNo");
//		    	String terNo=request.getParameter("terNo");
		    	
	    	    if(!servicesResultObjectOfgetPosTransactionDataListBy.isSuccess()){
	        		commonResponseDataJson.setSuccess(servicesResultObjectOfgetPosTransactionDataListBy.isSuccess());
	           		commonResponseDataJson.setMsg(servicesResultObjectOfgetPosTransactionDataListBy.getResultMsg());
	        		commonResponseDataJson.setData(null);
	        	}else{
	        		PaginatedInfor<PosTransactionDataStats> posTransactionDataPageInfor=servicesResultObjectOfgetPosTransactionDataListBy.getReturnObj();
	         		  
	        		List<PosTransactionDataStats> currrentPagePosTransactionDataList=posTransactionDataPageInfor.getDataList();
	        		int totalRecords=posTransactionDataPageInfor.getTotalRecords();
	        		int totalPages=1;
	    			if(totalRecords%pageSize!=0){
						totalPages=totalRecords/pageSize+1;
					}else{
						totalPages=totalRecords/pageSize;
					}
	    			
	    			if(startPage>totalPages){
	    				startPage=totalPages;
	    			}
	    			
	        		commonResponseDataJson.setMsg("");
	        		commonResponseDataJson.setSuccess(true);
	        		commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
	        		commonResponseDataJson.setData(currrentPagePosTransactionDataList);
	        		
	        		commonResponseDataJson.setRecordsPer(pageSize);
	        		commonResponseDataJson.setPageId(startPage);
	        		commonResponseDataJson.setTotalRecords(totalRecords);
	        		commonResponseDataJson.setTotalPage(totalPages);
	        		
	          		log.debug("getPosTradeStatsByCard json data22:"+JSONObject.fromObject(commonResponseDataJson,JSONObjectHelp.getJsonConfig()).toString());
	
	        	}
    		
    		
    	}catch(Exception exp){
    		exp.printStackTrace();
    		commonResponseDataJson.setResultCode(JsonResponseData.EXCEPTION);
    		commonResponseDataJson.setMsg(commonResponseDataJson.getMsg()+exp);
    	}
		
		return SUCCESS;
	}
	
    
}
